package gov.kqqzyyy.monkey.ydhl.ui;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ExpandableListView;

import java.lang.ref.WeakReference;
import java.util.List;

import adapter.XJJLListAdapter;
import adapter.XJYSListAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.ydhl.R;
import mytools.MyBaseActivity;
import objects.HE_XJJL_Detail;
import objects.HE_XJXX_Detail;

/*
* 健康宣教
* */
public class HeathEducationActivity extends MyBaseActivity implements ViewPager.OnPageChangeListener, SwipeRefreshLayout.OnRefreshListener, AdapterView.OnItemLongClickListener, BottomNavigationView.OnNavigationItemSelectedListener {

    @BindView(R.id.he_tab)
    TabLayout mTabLayout;
    @BindView(R.id.he_viewpager)
    ViewPager mViewPager;

    private List<HE_XJXX_Detail> mHEList;
    private List<HE_XJJL_Detail> mHERdList;
    private String mHEResult;
    private String mHERecordResult;

    private SwipeRefreshLayout mJKXJSwipe;
    private SwipeRefreshLayout mXJJLSwipe;
    private ExpandableListView mJKXJExpandable;
    private ExpandableListView mXJJLExpandable;
    private static BottomNavigationView bottomNavigationView;
    private XJYSListAdapter mXJYSAdapter;
    private XJJLListAdapter mXJJLAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_heath_education);
        ButterKnife.bind(this);

        mXJJLAdapter = new XJJLListAdapter(this);
        mXJYSAdapter = new XJYSListAdapter(this);
        HEPagerAdapter pagerAdapter = new HEPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(pagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        return false;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        return false;
    }

    @Override
    public void onRefresh() {

    }

    private class HEPagerAdapter extends FragmentPagerAdapter {

        HEPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            return super.instantiateItem(container, position);
        }

        @Override
        public Fragment getItem(int position) {
            return new HEPlaceholderFragment().newInstance(position);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "宣教操作";
                case 1:
                    return "宣教记录";
            }
            return null;
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }

    public static class HEPlaceholderFragment extends Fragment {

        private final String ARG_SECTION_NUMBER = "section_number";
        HeathEducationActivity activity;

        public HEPlaceholderFragment newInstance(int sectionNumber) {
            HEPlaceholderFragment fragment = new HEPlaceholderFragment();
            Bundle args = new Bundle();
            args.putInt(ARG_SECTION_NUMBER, sectionNumber);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            WeakReference<HeathEducationActivity> mActivity = new WeakReference<>((HeathEducationActivity) context);
            activity = mActivity.get();
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            int sectionNumber = getArguments().getInt(ARG_SECTION_NUMBER);
            View rootView;
            switch (sectionNumber) {
                case 0:
                    rootView = inflater.inflate(R.layout.activity_heath_educate, container, false);
                    activity.mJKXJSwipe = rootView.findViewById(R.id.he_swipe_layout);
                    activity.mJKXJExpandable = rootView.findViewById(R.id.he_expandable_lv);
                    bottomNavigationView = rootView.findViewById(R.id.he_bottom_menu);
                    activity.mJKXJSwipe.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
                    activity.mJKXJSwipe.setOnRefreshListener(activity);
                    bottomNavigationView.setOnNavigationItemSelectedListener(activity);
                    activity.mJKXJExpandable.setAdapter(activity.mXJYSAdapter);
                    break;
                case 1:
                    rootView = inflater.inflate(R.layout.activity_heath_educate_records, container, false);
                    activity.mXJJLSwipe = rootView.findViewById(R.id.he_records_swiptelv);
                    activity.mXJJLExpandable = rootView.findViewById(R.id.he_records_expandablelv);
                    activity.mXJJLSwipe.setColorSchemeResources(R.color.holo_green_dark, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light);
                    activity.mXJJLSwipe.setOnRefreshListener(activity);
                    activity.mXJJLExpandable.setAdapter(activity.mXJJLAdapter);
                    activity.mXJJLExpandable.setOnItemLongClickListener(activity);
                    break;
                default:
                    return super.onCreateView(inflater, container, savedInstanceState);
            }
            return rootView;
        }
    }
}
